import sys
sys.path.append('..')
import iris_data
import numpy as np
from sklearn.model_selection import train_test_split


def sigmoid(X, w):
    '''
    input X, w as the exponent of e
    X: np.array N*M
    w: vetro M*1
    return 0/1 N*1
    '''
    p = 1 / (1 + np.exp(-np.dot(X, w)))
    label_pre = np.where(p > 0.5, 1, 0)
    return label_pre

def logistic_regression(X_train, y_train, a=0.003, max_iter=500):
    '''
    input traning set, learning rate, max_iter
    a: learning rate. 
    max_iter: the maximum number of iterations
    '''
    n, m = X_train.shape
    w = np.ones((m, 1))
    _iter = 0
    while _iter < max_iter:
        _iter += 1
        error_matrix = y_train - sigmoid(X_train, w)
        w += a * np.dot(X_train.T, error_matrix)
    
    return w

def predict(X_test, w):
    return sigmoid(X_test, w)

X, y = iris_data.get_data()
#need to adjust y, cause the value of y is 1/-1
y = np.where(y == -1, 0, y)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)
w = logistic_regression(X_train, y_train, 0.003, 100)
res = sigmoid(X_test, w)

print ((res == y_test).sum() / len(y_test))